<?php
	/////////////////////////////////////////////////////////
	//Description: Task index view
	//Variable: 
	//		- $group_list: list of task group
	//Markup varibale
	//		- $project_id
	//		- $is_one_project
	//		- $g_id: group id
	//		- $task_i: current task id
	
	$project_id = Request::current()->param('project_id', NULL);
	$is_one_project = is_null($project_id) ? FALSE : TRUE;
	$g_id = 0;
	$t_count = count($tasks);
	if ($t_count == 0)
		$g_id = -1;
	else 
		$g_id = $tasks[0]['group_id'];
	$task_i = 0;
	$pivot = 0;
?>
<?php 
	echo View::factory('project/partials/top_control',array('project' => isset($cur_project) ? $cur_project : NULL ))
?>
<?php 
	//echo view::factory('task/partials/add_task', array('users' => $users, 'project' => $project));
?>
<div class='tstyle_content'>
	<?php echo View::factory('task/partials/add_task_none_list',  array('users' => $users, 'project' => $project, 'lists' => $lists))?>
	<?php if ($t_count != 0):?>
	<?php while($g_id != -1):?>
		<div class="list <?php echo $g_id?>">
		<a class="open-closed active" onClick="hide_show_group(event);" href="#"> Open/Closed </a>
		<h8><?php echo $tasks[$task_i]['group_name']?></h8>
		<div class="list-main-body">
			<table class="list <?php echo $g_id?> box-skin" style="width: 100%;">
				<tr class="rowA">
					<?php if(!$is_one_project):?><td><b>Project name</b></td><?php endif;?>
					<td><b>Tasks</b></td>
					<td><b>Assign to</b></td>
					<td><b>Assigned from</b></td>
					<td><b>Priority</b></td>
					<td><b>Deadline</b></td>
					<td><b>Status</b></td>
				</tr>
				<?php while($task_i == $pivot || ($task_i<$t_count && $tasks[$task_i]['group_id'] == $tasks[$task_i-1]['group_id'])):?>
					<?php $t = $tasks[$task_i]?>	
					<?php if ($t['status']==Helper_MyConst::TASK_ON_PROGRESS):?>
						<tr class="rowB jsmark-<?php echo $t['id']?>">
					<?php elseif ($t['status']==Helper_MyConst::TASK_DONE):?>
						<tr class="rowC jsmark-<?php echo $t['id']?>">
					<?php else:?>
						<tr class="rowC jsmark-<?php echo $t['id']?>">
					<?php endif;?>
							<td>
								<input type="checkbox" value="<?php echo $t['id']?>" onClick="close_task(event);" class="close-checkbox" > <?php echo $t['name']?>
								<div class="options">
									<a href="#" class="trigger">
										<img class="icon1" src="#">
									</a>
									<div class="sliding">
										<ul>
											<li><a class="edit-task" onClick="edit_task(event);" href="#">Edit</a></li>
											<li><a href="#">Move to</a></li>
											<li><a class="delete-task" onClick="delete_task(event);" href="#">Delete</a></li>
										</ul>
									</div>
								</div>
							</td>
							<td><?php echo $t['assign_user_displayname']?></td>
							<td><?php echo $t['user_displayname']?></td>
							<td><?php echo Helper_MyConst::$PROJECT_PRIORITY_TABLE[$t['priority']]?></td>
							<td><?php echo $t['deadline']?></td>
							<td><?php echo Helper_MyConst::$TASK_STATUS_TABLE[$t['status']]?></td>			
						</tr>
				<?php $task_i++;?>
				<?php endwhile;?>
				<?php
					echo view::factory('task/partials/add_task', array('users' => $users, 'project' => $project, 'group_id' => $g_id));
				?>
				<tr><td colspan="6"><a class="add-new-task" href="#">Add new task</a></td></tr>
			</table>
		</div>
		</div>
		<?php 
		if ($task_i<$t_count) 
		{
			$pivot = $task_i;
			$g_id = $tasks[$task_i]['group_id'];
		}
		else 
			$g_id = -1;
		?>
	<?php endwhile;?>
	<?php endif;?>
</div>
	

<script type="text/javascript">
	var tasks = $.parseJSON('<?php echo addslashes(json_encode($tasks))?>');
	make_task(tasks);

	var users = $.parseJSON('<?php echo addslashes(json_encode($users))?>');
	
	var allOptions = $.parseJSON('<?php echo addslashes(json_encode(Helper_MyConst::$TASK_OPTION_2_NAME))?>');
	var priority = $.parseJSON('<?php echo addslashes(json_encode(Helper_MyConst::$PROJECT_PRIORITY_TABLE))?>');
	
	$('input.data_option').change(function(){
		var table = $('div.tstyle_content table');
		var options = [];
		var names = [];
		
		//alert($('input.data_option:checked').length);
		$('input.data_option:checked').each(function() {
			   names.push($(this).attr('name'));
		       options.push($(this).val());
	     });
	    renderTable(options, names, tasks);
	});
	function renderTable(options, names, tasks)
	{
		var oLength = options.length;
		var dLength = tasks.length;
		var table = $('div.tstyle_content table');
		table.empty();
		var htmlStr = '';
		htmlStr+='<tr class="rowA">';
		for (var i = 0; i<oLength; i++)
			htmlStr+='<td><b>'+names[i]+'</b></td>';
		htmlStr+='</tr>';
		
		for (var i = 0; i<dLength; i++)
		{
			htmlStr+='<tr class="rowB">';
			for(var j = 0; j<oLength; j++)
			{
				if (options[j] == <?php echo Helper_MyConst::TASK_PROJECT_NAME?>)
					htmlStr+='<td><a href="/project/'+tasks[i].id+'">'+tasks[i][allOptions[options[j]]]+'</a></td>';
				else if(options[j] == <?php echo Helper_MyConst::TASK_STATUS?>) 
					htmlStr+='<td>'+assign_status[tasks[i][allOptions[options[j]]]]+'</td>';
				else if(options[j] == <?php echo Helper_MyConst::TASK_PRIORITY?>)
					htmlStr+='<td>'+priority[tasks[i][allOptions[options[j]]]]+'</td>'; 
				else
					htmlStr+='<td>'+tasks[i][allOptions[options[j]]]+'</td>';
			}
			htmlStr+='</tr>';
		}
		table.append(htmlStr);
	}

	//add task javascript handle
	function new_task()
	{
		$('div.top_control').hide();
		$('div.new-ele').show();
	}
	$('a#done_add_task').click(function(e){
		e.preventDefault();
		$('div.new-ele').hide();
		$('div.top_control').show();
	});
</script>